package view;

import javax.swing.ComboBoxModel;
import model.Line_Model;
import model.User_Model;
import ucm.UCRegisterOrders;
import ucm.UCUpdateAirline;
import ucm.UCUpdateReserve;
import ucm.UCUpdateSearch;

public class Air_User extends javax.swing.JFrame {

    private String[] airline;
    private String[] price;
    private String[] destination;
    private String[] linetime;
    private String[] gst;
    private ComboBoxModel cbm;
    private String name;

    /** Creates new form Air_User */
    public Air_User(String name) {
        initComponents();
        Update();
        Reserve();
        this.name = name;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        airlineComboBox = new javax.swing.JComboBox();
        airlineLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        priceTextField = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        destinationLabel = new javax.swing.JLabel();
        departLabel = new javax.swing.JLabel();
        destinationTextField = new javax.swing.JTextField();
        departTextField = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        updateButton = new javax.swing.JButton();
        noteTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        gst_Label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        reserve_List = new javax.swing.JList();
        searchTF = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        update_MenuItem = new javax.swing.JMenuItem();
        enrollMenuItem = new javax.swing.JMenuItem();
        delete_MenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        exitMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("User");

        airlineComboBox.setEditable(true);
        airlineComboBox.setFont(new java.awt.Font("Arial", 3, 12));
        airlineComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CA1116", "CA1111", "CA1112", "CA1113", "CA1114", "CA1115", "CA1117", "CA1118", "CA1119", "CA1110", "CA1120", "CA1121", "CA1122", "CA1123" }));
        airlineComboBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                airlineComboBoxMouseClicked(evt);
            }
        });
        airlineComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                airlineComboBoxActionPerformed(evt);
            }
        });

        airlineLabel.setFont(new java.awt.Font("Arial", 3, 14));
        airlineLabel.setText("Airline");

        priceLabel.setFont(new java.awt.Font("Arial", 3, 14));
        priceLabel.setText("Price");

        priceTextField.setEditable(false);
        priceTextField.setFont(new java.awt.Font("Arial", 3, 14));

        destinationLabel.setFont(new java.awt.Font("Arial", 3, 14));
        destinationLabel.setText("Destination");

        departLabel.setFont(new java.awt.Font("Arial", 3, 14));
        departLabel.setText("Depart Time");

        destinationTextField.setEditable(false);
        destinationTextField.setFont(new java.awt.Font("Arial", 3, 14));

        departTextField.setEditable(false);
        departTextField.setFont(new java.awt.Font("Arial", 3, 14));

        updateButton.setFont(new java.awt.Font("宋体", 3, 14));
        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        noteTextField.setEditable(false);
        noteTextField.setFont(new java.awt.Font("Arial", 3, 14));

        jButton1.setFont(new java.awt.Font("Arial", 3, 12));
        jButton1.setText("Buy");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("Gst:");

        gst_Label.setFont(new java.awt.Font("Arial", 1, 12));

        jScrollPane1.setViewportView(reserve_List);

        jButton2.setText("Destine Search");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        update_MenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        update_MenuItem.setText("Update");
        update_MenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                update_MenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(update_MenuItem);

        enrollMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        enrollMenuItem.setText("Enroll");
        enrollMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enrollMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(enrollMenuItem);

        delete_MenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        delete_MenuItem.setText("Delete");
        jMenu1.add(delete_MenuItem);
        jMenu1.add(jSeparator1);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.setText("Exit");
        jMenu1.add(exitMenuItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(airlineLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(airlineComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(priceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(destinationLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(destinationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(departLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addGap(16, 16, 16)
                .addComponent(departTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updateButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gst_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(noteTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 558, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(airlineComboBox)
                    .addComponent(airlineLabel)
                    .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destinationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(destinationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(departTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(departLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(updateButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(gst_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(noteTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void update_MenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_update_MenuItemActionPerformed
        Update();
}//GEN-LAST:event_update_MenuItemActionPerformed

    private void enrollMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enrollMenuItemActionPerformed
}//GEN-LAST:event_enrollMenuItemActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        Update();
    }//GEN-LAST:event_updateButtonActionPerformed

    private void airlineComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_airlineComboBoxActionPerformed
        int num = airlineComboBox.getSelectedIndex();
        if (num != -1) {
            //System.out.println(num);
            this.priceTextField.setText(price[num]);
            this.destinationTextField.setText(destination[num]);
            this.departTextField.setText(linetime[num]);
        }
    }//GEN-LAST:event_airlineComboBoxActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        UCRegisterOrders ucro = new UCRegisterOrders(this);
        ucro.run();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void airlineComboBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_airlineComboBoxMouseClicked
        int num = airlineComboBox.getSelectedIndex();
        //System.out.println(num);
        this.priceTextField.setText(price[num]);
        this.destinationTextField.setText(destination[num]);
        this.departTextField.setText(linetime[num]);
        this.gst_Label.setText(gst[num]);
    }//GEN-LAST:event_airlineComboBoxMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String[] str;
        String[] s = new String[3];
        if ((str = new UCUpdateSearch(this.searchTF.getText()).run()) != null) {
            System.out.println(str.length);
            this.airlineComboBox.removeAllItems();
            airline = new String[str.length];
            price = new String[str.length];
            destination = new String[str.length];
            linetime = new String[str.length];
            gst = new String[str.length];
            for (int count = 0; count < str.length; count++) {
                s = str[count].split(";");

                airline[count] = s[0];
                this.airlineComboBox.addItem(s[0]);
                price[count] = s[1];
                destination[count] = s[2];
                linetime[count] = s[3];
                gst[count] = s[4];
            }
            this.priceTextField.setText(price[0]);
            this.destinationTextField.setText(destination[0]);
            this.departTextField.setText(linetime[0]);
            this.gst_Label.setText(gst[0]);
        } else {
            this.noteTextField.setText("Nothing can be updated from database.");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    public void Update() {
        String[] str;
        String[] s = new String[3];
        if ((str = new UCUpdateAirline().run()) != null) {
            System.out.println(str.length);
            this.airlineComboBox.removeAllItems();
            airline = new String[str.length];
            price = new String[str.length];
            destination = new String[str.length];
            linetime = new String[str.length];
            gst = new String[str.length];
            for (int count = 0; count < str.length; count++) {
                s = str[count].split(";");

                airline[count] = s[0];
                this.airlineComboBox.addItem(s[0]);
                price[count] = s[1];
                destination[count] = s[2];
                linetime[count] = s[3];
                gst[count] = s[4];
            }
            this.priceTextField.setText(price[0]);
            this.destinationTextField.setText(destination[0]);
            this.departTextField.setText(linetime[0]);
            this.gst_Label.setText(gst[0]);
        } else {
            this.noteTextField.setText("Nothing can be updated from database.");
        }
    }

    public void Reserve() {
        String[] str;
        String[] s = new String[3];
        if ((str = new UCUpdateReserve(this).run()) != null) {
            System.out.println(str.length);
            this.reserve_List.removeAll();


            airline = new String[str.length];
            price = new String[str.length];

            for (int count = 0; count < str.length; count++) {
                s = str[count].split(";");
                airline[count] = s[1];
                price[count] = s[2];
            }
            this.reserve_List.setListData(airline);
        } else {
            this.noteTextField.setText("Nothing can be updated from database.");
        }
    }

    public User_Model getUserModel() {
        User_Model um = new User_Model();
        um.setUsername(name);
        return um;
    }

    public Line_Model getLineModel() {
        Line_Model lm = new Line_Model();
        lm.setDestination(this.destinationTextField.getText());
        if (this.airlineComboBox.getSelectedIndex() == -1) {
            lm.setName(String.valueOf(this.airlineComboBox.getItemAt(0)));
            System.out.println(this.airlineComboBox.getItemAt(0));
        } else {
            lm.setName(String.valueOf(this.airlineComboBox.getSelectedItem()));
            System.out.println(String.valueOf(this.airlineComboBox.getSelectedItem()));
        }
        int price = Integer.valueOf(this.priceTextField.getText());
        price = price * 10 / 100 + price;
        lm.setPrice(String.valueOf(price));
        lm.setTime(this.departTextField.getText());
        return lm;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new Air_User().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox airlineComboBox;
    private javax.swing.JLabel airlineLabel;
    private javax.swing.JMenuItem delete_MenuItem;
    private javax.swing.JLabel departLabel;
    private javax.swing.JTextField departTextField;
    private javax.swing.JLabel destinationLabel;
    private javax.swing.JTextField destinationTextField;
    private javax.swing.JMenuItem enrollMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JLabel gst_Label;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField noteTextField;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JTextField priceTextField;
    private javax.swing.JList reserve_List;
    private javax.swing.JTextField searchTF;
    private javax.swing.JButton updateButton;
    private javax.swing.JMenuItem update_MenuItem;
    // End of variables declaration//GEN-END:variables
}
